<html>
<head><meta charset="utf-8"><title>rust-analyzer#907 · t-compiler/rust-analyzer · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/index.html">t-compiler/rust-analyzer</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/rust-analyzer.23907.html">rust-analyzer#907</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="159814259"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/rust-analyzer%23907/near/159814259" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/rust-analyzer.23907.html#159814259">(Mar 02 2019 at 20:53)</a>:</h4>
<p>hi</p>



<a name="159868730"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/rust-analyzer%23907/near/159868730" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> John Renner <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/rust-analyzer.23907.html#159868730">(Mar 03 2019 at 22:56)</a>:</h4>
<p>hi! <br>
Okay so digging into this genericparams thing. I think we need to improve the id we return in <code>Resolution::GenericParam(u32)</code>because it returns the index at _some_ unknown scope. which seems not what we want</p>



<a name="159892984"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/rust-analyzer%23907/near/159892984" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/rust-analyzer.23907.html#159892984">(Mar 04 2019 at 07:37)</a>:</h4>
<p>Hm, I think keeping it just <code>u32</code> should be fine, <em>but</em> we need to change the <code>Resolver</code> to return the generic scope as well</p>



<a name="159893040"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/rust-analyzer%23907/near/159893040" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/rust-analyzer.23907.html#159893040">(Mar 04 2019 at 07:38)</a>:</h4>
<p>Like, the <code>ExprId</code> is also just an <code>u32</code>, but the resolver has a <code>body</code> method whihc returns the relevant scope</p>



<a name="159893046"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/rust-analyzer%23907/near/159893046" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/rust-analyzer.23907.html#159893046">(Mar 04 2019 at 07:38)</a>:</h4>
<p>so, we can add <code>.generics</code> method to resolver, which adds info about generics</p>



<a name="160253452"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/rust-analyzer%23907/near/160253452" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> John Renner <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/rust-analyzer.23907.html#160253452">(Mar 08 2019 at 00:56)</a>:</h4>
<p>That makes sense. I'll get cracking on that. Just a question, while we're at it, why not make body and generics part of the <code>Resolution::{LocalBinding, Generic}</code>. It seems odd that they methods are so linked to the specific resolver states, but not exposed that way.</p>



<a name="160268838"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/rust-analyzer%23907/near/160268838" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/rust-analyzer.23907.html#160268838">(Mar 08 2019 at 07:10)</a>:</h4>
<p>That won't work automatically: you'll have to add lifetime parameters or add Arcs.... But in general this seems like a good idea: keep <code>LocalBindint</code> <em>private</em> to the hir crate and make a higher-level API on top of that for IDEs, sort-of like stuff in <code>code_model</code> works</p>



<a name="160268843"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/rust-analyzer%23907/near/160268843" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/rust-analyzer.23907.html#160268843">(Mar 08 2019 at 07:10)</a>:</h4>
<p>hm, I think if we use code-model pattern exactly, we'll get something like this</p>



<a name="160268909"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/rust-analyzer%23907/near/160268909" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/rust-analyzer.23907.html#160268909">(Mar 08 2019 at 07:13)</a>:</h4>
<div class="codehilite"><pre><span></span>pub struct LocalBinding {
    pat: PatId
}

impl LocalBinding {
     pub fn syntax(self, resolver: &amp;Resolver) -&gt; TreeArc&lt;SyntaxPtr&gt; {
        resolver.body.source_map().pat_synax(self.id)
     }
}

impl Resolver {
   /*private*/ fn body()....
}
</pre></div>



<a name="160321874"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/rust-analyzer%23907/near/160321874" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> John Renner <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/rust-analyzer.23907.html#160321874">(Mar 08 2019 at 20:18)</a>:</h4>
<p>That's a bit better, but it still has this property that you need to call <code>LocalBinding::syntax</code> with the same <code>Resolver</code> that produced it, otherwise you could get nonsense results.</p>



<a name="160324917"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/rust-analyzer%23907/near/160324917" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> John Renner <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/rust-analyzer.23907.html#160324917">(Mar 08 2019 at 20:57)</a>:</h4>
<p>Though on further reflection, I'm not sure exactly what we could do better. Maybe debug asserts that they match? but anyway this is a bit of a rabbit-hole. I think the code-model pattern makes a good deal of sense</p>



<a name="160333695"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/rust-analyzer%23907/near/160333695" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> John Renner <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/rust-analyzer.23907.html#160333695">(Mar 08 2019 at 22:49)</a>:</h4>
<p>Okay more digging leads me to think that generalizing Body and BodySourceMap structure may be beneficial here, because ultimately the types all work the same way, they're just constructed differently on a per item basis. Is the underlying id for PatId, ExprId, ImplId, etc. guaranteed to be unique? That is, could we unify them with a <code>EntityId</code>or some such thing so that we could store them in a single map? Cuz then we could provide methods to query for the specific <code>ast::</code> node you're looking for.</p>



<a name="160371184"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/rust-analyzer%23907/near/160371184" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/rust-analyzer.23907.html#160371184">(Mar 09 2019 at 15:12)</a>:</h4>
<p>I think that long-term we do want to generalize this source-map pattern, but I am not sure it's best to do it right now. I think haven't got enough experience using them, so we are at risk of extracting a wrong abstraction .</p>
<p>Similarly, I feel uneasy about generic <code>EntityId</code> : it is possible, but we'll lose some type-safety that way. I would lean towards a type-safe trait-based solution, like <code>AstItemDef</code></p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>